package com.yam.michael.hibernate.dao.impl;

import java.io.Serializable;
import java.sql.SQLException;
import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.xunjia.exception.XunjiaException;
import com.yam.michael.hibernate.dao.UserInfoDao;
import com.yam.michael.hibernate.pojo.UserInfo;

public class UserInfoDaoImpl extends HibernateDaoSupport implements UserInfoDao, Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 2906398438662969753L;
	// 日志
	private static final Logger logger = Logger.getLogger(UserInfoDaoImpl.class);

	@Override
	/*
	 * 注册用户
	 * 
	 * @param userInfo
	 * 
	 * @return
	 * 
	 * @throws XunjiaException
	 */
	@Transactional(isolation = Isolation.SERIALIZABLE, propagation = Propagation.REQUIRED, rollbackFor = { XunjiaException.class })
	public UserInfo addUserInfo(UserInfo userInfo, String type) throws XunjiaException {
		try {
			HibernateTemplate template = this.getHibernateTemplate();
			Long id = (Long) template.save(userInfo);
			String loginName = type + id;
			System.out.println(loginName);
			userInfo.setLoginName(loginName);
			userInfo.setID(id);
			template.saveOrUpdate(userInfo);
			return (UserInfo) template.get(UserInfo.class, id);
		} catch (DataAccessException e) {
			logger.info(e.getMessage());
			throw new XunjiaException("添加用户发生错误,添加失败!!!");
		}
	}

	@Override
	/*
	 * 删除用户信息
	 * 
	 * @param userInfo
	 * 
	 * @throws XunjiaException
	 */
	public void deleteUserInfo(UserInfo userInfo) throws XunjiaException {
		try {
			this.getHibernateTemplate().delete(userInfo);
		} catch (DataAccessException e) {
			logger.info(e.getMessage());
			throw new XunjiaException("删除用户发生错误,删除失败!!!");
		}
	}

	@Override
	/*
	 * 按ID获得用户信息
	 * 
	 * @param userId
	 * 
	 * @return
	 * 
	 * @throws XunjiaException
	 */
	public UserInfo getUserInfoById(final String userId) throws XunjiaException {
		try {
			return (UserInfo) this.getHibernateTemplate().execute(new HibernateCallback() {
				@Override
				public Object doInHibernate(Session session) throws HibernateException, SQLException {
					Query q = session.createQuery("FROM UserInfo AS u WHERE u.loginName=?");
					q.setString(0, userId);
					return q.uniqueResult();
				}
			});
		} catch (DataAccessException e) {
			logger.info(e.getMessage());
			throw new XunjiaException("按ID获取用户信息发生错误,获取失败!!!");
		}
	}

	@Override
	/*
	 * 更新用户信息
	 * 
	 * @param userInfo
	 * 
	 * @throws XunjiaException
	 */
	public void updateUserInfo(UserInfo userInfo) throws XunjiaException {
		try {
			this.getHibernateTemplate().saveOrUpdate(userInfo);
		} catch (DataAccessException e) {
			logger.info(e.getMessage());
			throw new XunjiaException("更新用户发生错误,更新失败!!!");
		}
	}

}
